package com.gk.controller.api;

import com.alipay.api.AlipayApiException;
import com.eight.pojo.UserCharge;
import com.eight.util.TimeUtil;
import com.eight.util.WeChatPayUtil;
import com.eight.vo.*;
import com.gk.dao.UserChargeDao;
import com.gk.fein.ChangeRabbitFergn;
import com.gk.service.UserChargeService;
import com.gk.uitl.GetZFBNative;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * 二维码生成操作控制层
 * @Description: 用户进行支付操作的Controller
 * @Author: Van.Planifolia
 * @CreateTime: 2023-03-04  09:12
 * @Version: 1.0
 */
@RestController
public class PayApi {
    // 注入用户充值的service层
    @Autowired
    private UserChargeService userChargeService;

    @Autowired
    private ChangeRabbitFergn changeRabbitFergn;

    @Autowired
    private UserChargeDao userChargeDao;

    @PostMapping("/customer/api/createNative/")
    public ComResult createNative(@RequestBody WeChatPay weChatPay) {
        return userChargeService.createNative(weChatPay);
    }


    /**
     * 微信二维码生成
     * @param uid    账号
     * @param amount 金额
     * @return 生成二维码
     */
    @PostMapping("/customer/api/createNativetwo/{amount}/{uid}")
    public ComResult createNative2(@PathVariable Integer uid, @PathVariable BigDecimal amount) {
            UserCharge userCharge = userChargeService.selectOneUserCharge(uid);
            WeChatComData weChatComData = new WeChatComData();
            weChatComData.setAmount(amount);
            weChatComData.setSerialNumber(userCharge.getSerilcode());
            weChatComData.setTitle(userCharge.getSerilcode());
            WeChatComData weChatComData1 = WeChatPayUtil.makePayOrder(weChatComData);

        if(weChatComData1!=null){
                return new ComResult(2000, "请支付", weChatComData1);
            }else{
                return new ComResult(5000, "订单超时");
            }
    }


    /**
     * 通过随机订单生成支付宝二维码
     * @param  orderPayVo 金额
     * @return 1
     */
    @PostMapping("/customer/api/charge/getZFBNativeCodeUrl/")
    public ComResult getZFBNativeCodeUrl(@RequestBody OrderPayVo orderPayVo){
        //生成订单号
        String s = UUID.randomUUID().toString();
        orderPayVo.setOrderNo(s);
        //生成二维码
        GetZFBNative getZFBNative =new GetZFBNative();
        String s1 = getZFBNative.aliPay(orderPayVo);
        //rabbit添加订单号
        changeRabbitFergn.makeChargeOrder(orderPayVo.getOrderNo());
        //添加订单
        UserCharge userCharge = new UserCharge();
        userCharge.setState(0);
        userCharge.setSerilcode(orderPayVo.getOrderNo());
        userCharge.setCreatetime(TimeUtil.now(""));
        userCharge.setUcamount(BigDecimal.valueOf(orderPayVo.getAmount()));
        userCharge.setUname(orderPayVo.getUsername());
        userCharge.setZid(1);
        userChargeDao.insert(userCharge);

        //返回给前端信息
        ErWeimaVO erWeimaVO = new ErWeimaVO();
        erWeimaVO.setUrl(s1);
        erWeimaVO.setId(s);
        return  new ComResult(2000,"查询成功",erWeimaVO);
    }


    /**
     * 根据订单号查询订单状态
     * @param id  订单号
     * @return 1
     */
    @GetMapping("/customer/api/charge/getPayStatusTwo/{id}")
    public ComResult selectTwoUserCharge(@PathVariable String id) throws AlipayApiException {
        return   userChargeService.selectTwoUserCharge(id);
    }


    /**
     * 根据订单号生成支付宝二维码
     * @param amount 金额
     * @param uid 订单序列号
     * @return 1
     */
    @PostMapping("customer/api/createNativeThree/{amount}/{uid}")
    public ComResult createNativeThree(@PathVariable Double amount,@PathVariable Integer uid ){
        return userChargeService.createNativeThree(amount,uid);
    };
}
